﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class PlayerAcion : Player {

    public static PlayerAcion instance;
    public static PlayerAcion Instance
    {
        get
        {
            return instance;
        }
    }

	[Serializable]
	public class CountDows
	{
		public float defaultCD;
		[Space(10)][Header("Value")]
		public float CurCountDown;
		public Finder.FindType Type;
		public float CDFix;
	}
	[SerializeField]	[Space(20)]
	public CountDows[] CD;
    //探索 殖民 毁灭
    private void Awake()
    {
        instance = this;
    }
    void Update(){
        if(PlayerID!=1)
        {
            return;
        }
		if (ControlCenter.Instance.Players [PlayerID - 1].GetComponent<Player> ().CurState.OwnedSystems > 0 && ControlCenter.Instance.Players [PlayerID - 1].GetComponent<Player> ().CurState.ResourceInStore >0)
			DoSth ();
		else
			Die();

		if (CurState.ResourceInStore > CurState.MaxRecource) {
			CurState.ResourceInStore = CurState.MaxRecource;
			CD[0].CurCountDown = 0;
			CD[1].CurCountDown = 0;
			CD[2].CurCountDown = 0;
		}
	}
	void Die(){
		gameObject.name = "die";
		GameObject[] Systems;
		Systems = GameObject.FindGameObjectsWithTag ("systems");
		foreach (GameObject sys in Systems) {
			if (sys.GetComponent<StarSystem> ().Owner == PlayerID) {
				sys.GetComponent<StarSystem> ().Owner = 0;
				sys.GetComponent<StarSystem> ().State_ = StarSystem.States.origin;
			}
		}
	}
	void DoSth(){
		for (int i = 0; i < CD.Length; i++) {
			SetFix (i);
			CD[i].CurCountDown -= Time.deltaTime; 
			if (CD [i].CurCountDown <= 0) {
				CD [i].CurCountDown = CD [i].defaultCD * CD [i].CDFix * UnityEngine.Random.Range(0.8f,1.2f) * ControlCenter.Instance.YearRate;
				Action (CD [i].Type);
			}
		}
	}
	void SetStart(){
		for (int i = 0; i < CD.Length; i++) {
			SetFix (i);
			CD[i].CurCountDown -= Time.deltaTime; 
			if (CD [i].CurCountDown < 0) {
				CD [i].CurCountDown = CD [i].defaultCD * CD [i].CDFix * UnityEngine.Random.Range(0.8f,1.2f) * ControlCenter.Instance.YearRate;
			}
		}
	}
	void SetFix(int i)//行为冷却修正
	{
		switch (i) 
		{
		case 0://探索
			CD [0].CDFix = (CD [1].CDFix * 2) / CD [2].CDFix;
			if (CurState.EnemySystemCount == 0)
				CD [0].CDFix = 1;
			CD [0].CDFix *= ControlCenter.Instance.GameValue;
			break;			
		case 1://殖民 与拥有星系相关
			CD[1].CDFix = 0.8f +0.25f * CurState.OwnedSystems;
			break;			
		case 2://毁灭 与敌对星系数量相关
			CD [2].CDFix = 2 - CurState.EnemySystemCount * 0.2f;
			if (CD [2].CDFix < 0.5f)
				CD [2].CDFix = 0.5f;
			CD [2].CDFix *= ControlCenter.Instance.GameValue;
			break;
		default:
			break;
		}
	}
	void Start(){
		for (int i = 0; i < CD.Length; i++) {
			CD [i].CurCountDown = CD [i].defaultCD * UnityEngine.Random.Range(0.8f,1.2f) * ControlCenter.Instance.YearRate;
		}
			
		CurState.ResourceInStore = 100;
		CurState.EnemySystemCount = 0;
		CurState.OwnedSystems = 1;
	}
	public void RePrePare(Finder.FindType Type){
		switch (Type) 
		{
		case Finder.FindType.explore:
			CD[0].CurCountDown = 10;
			CD[1].CurCountDown = 5;
			CD[2].CurCountDown = 7;
			break;			
		case Finder.FindType.colony:
			CD[0].CurCountDown = 7;
			CD[1].CurCountDown = 10;
			CD[2].CurCountDown = 5;
			break;			
		case Finder.FindType.enemy:
			CD[0].CurCountDown = 4;
			CD[1].CurCountDown = 6;
			CD[2].CurCountDown = 10;
			break;
		default:
			break;
		}
	}
}
